/*
 *  AComposition.cpp
 *  libavid
 *
 *  Created by Peter on 24.02.12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "AComposition.h"
#include "AStream.h"
#include "AObjDoc.h"

#include "AMediaDesc.h"

#include <iomanip>
#include <ctime>

A_IMPLEMENT_CLASS( 'CMPO', AComposition, ATrackGroup );

void AComposition::readFromStream( AStream& str, const AObjDoc& objd )
{
	ATrackGroup::readFromStream( str, objd );
	
	UInt8 tagMark, version;
	str.readPod( tagMark );
	str.readPod( version );
	TR_ASSERT( tagMark == 0x02 );
	TR_ASSERT( version == 0x02 );
	
	str.readPod( _mobIdHigh );
	str.readPod( _mobIdLow );
	str.readPod( _lastModified );
	str.readPod( _mobType );
	str.readPod( _usageCode );
	
	UInt32 physicalMediaRef;
	str.readPod( physicalMediaRef );
	
	_physicalMedia = objd.resolveRef< AMediaDesc >( physicalMediaRef );
	
	// On to the PClass Extensions
	str.readPod( tagMark );
	str.readPod( version );
	TR_ASSERT( tagMark == 0x01 );
	TR_ASSERT( version == 0x01 );
	
	// Creation time?
	str.readPod( tagMark );
	TR_ASSERT( tagMark == 71 );
	str.readPod( _creationTime );
	
	readMobIdFromAObjDocStream( str,_mobId );
	
	// End tag marker
	str.readPod( tagMark );
	TR_CHECK( tagMark == 0x03 );	
}

#define TRACE_FIELD( x ) "\t" #x ": " << x << std::endl
#define TRACE_CHAR_FIELD( x ) "\t" #x ": " << static_cast< int >( x ) << std::endl

void AComposition::dump( std::ostream& str ) const
{
	// We should probably store the data in a member variable of type time_t
	// in the first place
	time_t t = _lastModified;
	
	str << "AComposition {" << std::endl;
	str << "\t";
	
	ATrackGroup::dump( str );
	
	str << TRACE_FIELD( _mobIdHigh );
	str << TRACE_FIELD( _mobIdLow );
	
	str << "\t_lastModified: " << ::ctime( &t ); // Note: ctime causes a newline
	str << "\t_mobType: " << static_cast< int >( _mobType ) << std::endl;
	
	str << TRACE_FIELD( _usageCode );
	str << TRACE_FIELD( _physicalMedia );
	
	t = _creationTime;
	str << "\t_creationTime: " << ::ctime( &t ); // Note: ctime() causes a newline
	
	str << TRACE_FIELD( _mobId );
	
	str << "}" << std::endl;
}